package com.gombe.credit.app.ui.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.gombe.credit.app.R
import com.gombe.credit.app.common.CommonDialog
import com.gombe.credit.app.databinding.DialogGombeCreditLimitIncreasedBinding
import com.gombe.credit.app.event.GombeCreditEventPointType
import org.json.JSONObject
import java.math.BigDecimal


/**
 * GombeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：产品额度提升dialog
 */
class DilaogGombeCreditLimitIncreased: CommonDialog {
    private lateinit var binding: DialogGombeCreditLimitIncreasedBinding
    private var mCallback: IDialogCallback? = null
    private var eventRemark = ""

    constructor(context: Context) : super(context) {}
    constructor(context: Context, eventRemark: String, callback: IDialogCallback?) : super(context) {
        this.eventRemark = eventRemark
        mCallback = callback
    }

    override fun initGombeCreditWindow() {
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window?.setGravity(Gravity.CENTER)
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun takeGombeCreditRootView(): View {
        binding = DialogGombeCreditLimitIncreasedBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initGombeCreditView() {

    }

    override fun initGombeCreditListener() {
        setOnClickListener(binding.buttonApplyNow) //申请
        setOnClickListener(binding.imageLimitClose) //关闭
    }

    override fun takeGombeCreditData() {
    }

    override fun onGombeCreditViewClick(v: View?) {
        dismiss()
        if (v?.id == R.id.buttonApplyNow){
            mCallback?.onClick(v)
        }
    }

    /**
     * 设置数据
     */
    fun showGombeCreditLimitIncreasedData(data: JSONObject){
        binding.textImproveAmount.text = BigDecimal(data.optString("amount")).stripTrailingZeros().toPlainString()
        val text = BigDecimal(data.optString("oldAmount")).stripTrailingZeros().toPlainString()
        binding.textOldAmount.text = text
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_DIALOG.LIMIT_INCREASED_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_DIALOG.LIMIT_INCREASED_DISMISS,eventRemark)
    }


    /**
     *
     * 接口描述：product额度相较比于上一次是否有变化
     * 接口名：/v1/amountIsUp
     * 参数：公共参数
     * 注意：贷超包product和token参数，传所需要查询的产品对应的值
     * 返回值：
     * openFlag，值"YES"或"NO"
     * 当openFlag=“YES”时，会多两个返回值：amount、oldAmount
     *
     */

}